package com.wuhunyu.servicebase.result;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wuhunyu.commonutils.result.CommonResult;
import com.wuhunyu.servicebase.exception.Assert;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 响应网页的结果类
 *
 * @author wuhunyu
 * @version 1.0
 * @date 2021-01-29 19:10
 */
public class ResponseUtil {

    public static void out(HttpServletResponse response, CommonResult commonResult) {
        ObjectMapper mapper = new ObjectMapper();
        response.setStatus(HttpStatus.OK.value());
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        try {
            mapper.writeValue(response.getWriter(), commonResult);
        } catch (IOException e) {
            Assert.isRight(true, "响应结果IO异常");
        }
    }

}
